spolecznoscfandomcom-20200214-history
Użytkownik:Nanaki/unlicensed.py
Bot który utworzy listę plików które nie posiadają w opisie żadnego szablony użytego na Mediawiki:Licenses. Domyślnie lista wyląduje w pliku unlicensed-{family}-{lang}.txt Źródło # -*- coding: utf-8 -*- """ These command line parameters can be used to specify which pages to work on: &params; These command line parameters can be used to specify various aspects of the bot: -listfile: File to witch save the list (default: unlicensed-{family}-{lang}.txt) -append Append found files to an existing list (rather than creating new list) -fast Checks only templates used directly in the description (not templates inside templates) -empty Filter out non-empty pages (empty means 4 characters or less content - not including interwiki and categories) -noempty Opposite of the above """ # # © Karol Dylewski 2013 # # Distributed under the terms of the CC-BY-SA 3.0 license. import wikipedia as pywikibot import pagegenerators import json, codecs # This is required for the text that is shown when you run this script # with the parameter -help. docuReplacements = { '&params;': pagegenerators.parameterHelp, } def getLicenseTemplates(page): global site templates = [] try: text = page.getOldVersion(page.latestRevision()).split('\n') except pywikibot.NoPage: text = site.mediawiki_message('Licenses').split('\n') for line in text: parts = line.lstrip(' \t\n\r*#').split(u'|'):-1 for part in parts: part = part.strip() if part.find('=') != -1: continue if part.lower().startswith('subst:'): continue if part not in templates: templates.append(part) pywikibot.output(u'\nFound \03{lightaqua}%d\03{default} license templates' % len(templates)) templates2 = [] for template in templates: title = template if title.find(':') -1: title = 'Template:'+title tpl = pywikibot.Page(site, title) pywikibot.output(' %s' % tpl) if tpl not in templates: templates2.append(tpl) return templates2 def getPageTemplates(page): global site apiTemplates = site.apipath() + '?action=query&format=json&prop=templates&titles=' try: response = site.getUrl(apiTemplates + page.urlname()) obj = json.loads(response)'query''pages' for pageid in obj: if page.title() != objpageid'title': continue templates = [] tpls = objpageid'templates' for tpl in tpls: template = pywikibot.Page(site, tpl'title', defaultNamespace = tpl'ns') templates.append(template) return templates except KeyError: return [] def main(*args): genFactory = pagegenerators.GeneratorFactory() genFactory.namespaces.extend(6) # Read commandline parameters. empty = None append = False filename = None fast = False for arg in pywikibot.handleArgs(*args): if arg '-append': append = True elif arg '-fast': fast = True elif arg '-empty': empty = True elif arg '-noempty': empty = False elif arg.startswith('-listfile:'): filename = arg10: else: if not genFactory.handleArg(arg): pywikibot.showHelp() break global site site = pywikibot.getSite() licenseTemplates = getLicenseTemplates(pywikibot.Page(site, u'MediaWiki:Licenses')) gen = genFactory.getCombinedGenerator() if gen None: gen if filename None: filename = 'unlicensed-%s-%s.txt' % (site.family.name, site.lang) pywikibot.output(u'\n\nFile for the list: %s' % filename) if append: f = codecs.open(filename, "a", "utf-8") else: f = codecs.open(filename, "w", "utf-8") count = 0 for page in gen: pywikibot.output(u'\n\03{lightyellow}%s\03{default}' % page) if not page.isImage(): continue if fast: usedTemplates = [] templates = page.templates() for tpl in templates: p = pywikibot.Page(site, 'Template:'+tpl, defaultNamespace = 10) usedTemplates.append(p) else: usedTemplates = getPageTemplates(page) for template in usedTemplates: pywikibot.output(u' %s' % template) hasLicense = False for tpl in usedTemplates: if tpl in licenseTemplates: hasLicense = True break if not hasLicense: pywikibot.output(u' \03{lightred}No license\03{default}') count = count + 1 if empty None: f.write(u'%s\n'%page) else: if empty and page.isEmpty(): f.write(u'%s\n'%page) if not empty and not page.isEmpty(): f.write(u'%s\n'%page) else: pywikibot.output(u' \03{lightgreen}License found\03{default}') pywikibot.output(u'\n\nFound \03{lightaqua}%d\03{default} file%s without license specified' % (count, ('s','')count 1)) if __name__ "__main__": try: main() finally: pywikibot.stopme()